package com.wuhunyu.controller;


import com.wuhunyu.commonutils.result.CommonResult;
import com.wuhunyu.query.DataQuery;
import com.wuhunyu.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author wuhunyu
 * @since 2021-01-27
 */
@RestController
@RequestMapping("/statisticsDaily")
//@CrossOrigin
public class StatisticsDailyController {

    @Autowired
    private StatisticsDailyService statisticsDailyService;

    @PostMapping("/count/{date}")
    public CommonResult countUser(@PathVariable("date") Date date) {
        return statisticsDailyService.countUser(date);
    }

    @PostMapping("/showData")
    public CommonResult showData(@RequestBody(required = false) DataQuery dataQuery) {
        return statisticsDailyService.showData(dataQuery);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        // 转换日期 注意这里的转化要和传进来的字符串的格式一直 如2019-01-21 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // CustomDateEditor为自定义日期编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

}

